package org.geotools.toolbox;

import java.io.File;
import java.io.IOException;

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.gce.geotiff.GeoTiffFormat;
import org.geotools.gce.geotiff.GeoTiffWriteParams;
import org.geotools.gce.geotiff.GeoTiffWriter;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.process.vector.HeatmapProcess;
import org.geotools.utils.FileUtils;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.util.ProgressListener;

public class Shp2HeatMap {
	
	public static GridCoverage2D execute(String inpath, String field_name,Integer radius_pixel, String outpath,
			ReferencedEnvelope bounds,Integer outputWidth, Integer outputHeight) throws IOException {
		System.out.println("Beginning Shp2HeatMap Toolbox execution....");
		File indata=new File(inpath);
		FileDataStore store = FileDataStoreFinder.getDataStore(indata);
		System.out.println("Input Data:"+inpath);
		SimpleFeatureSource featureSource = store.getFeatureSource();
		SimpleFeatureCollection features = featureSource.getFeatures();
		if(radius_pixel==null){
			radius_pixel=100;
			System.out.print("using defalut radius(pixel)");
			System.out.println("...radius="+radius_pixel);
		}
		if(bounds==null){
			bounds = features.getBounds();
		}		
		if(outputWidth==null){
			outputWidth=8000;
		}
		if(outputHeight==null){
			outputHeight=8000;
		}
		String covName = "Test";
		ProgressListener monitor = null;
        HeatmapProcess process = new HeatmapProcess();
        GridCoverage2D cov = process.execute(features, // data
        		radius_pixel,  //radius
                field_name, // weightAttr
                1, // pixelsPerCell
                bounds, // outputEnv
                outputWidth, // outputWidth
                outputHeight, // outputHeight
                monitor // monitor)
        );        
        if(outpath!=null){
        	System.out.print("writing result..."); 
        	FileUtils.exportRasterfile(cov, outpath);
             System.out.println("done writing result!");
        }
       return (cov);
	}
}
